/*----------------------------------------------------------------------------*/
/* Copyright 2011 http://www.kmftech.com                                      */
/*                                                                            */
/* This file is part of kmftech.                                              */
/*                                                                            */
/* kmftech is free software: you can redistribute it and/or modify it under   */
/* the terms of the GNU General Public License as published by the Free       */
/* Software Foundation, version 2 of the License, or (at your option) any     */
/* later version.                                                             */
/*                                                                            */
/* kmftech is distributed in the hope that it will be useful, but WITHOUT ANY */
/* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  */
/* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more      */
/* details.                                                                   */
/*                                                                            */
/* You should have received a copy of the GNU General Public License along    */
/* with kmftech. If not, see http://www.gnu.org/licenses/.                    */
/*----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------*/
/*---------------------------- DOCUMENT HISTORY ------------------------------*/
/* Ver: 1.0    Author: Karim Mansour                      Date: 07 June  2011 */
/* Ver: 1.1    Author: Karim Mansour                      Date: 08 June  2011 */
/* Ver: X.X    Author:                                    Date:               */
/*----------------------------------------------------------------------------*/
/*---------------------    DOCUMENT CHANGE HISTORY ---------------------------*/
/* Ver: 1.0    Change: Initial Version.                                       */
/* Ver: 1.1    Change: Fixed bug of division and function header              */
/* Ver: X.X    Change:                                                        */
/*----------------------------------------------------------------------------*/


/*                           Needed Header Files                              */
#include <avr/interrupt.h>
#include <avr/io.h>
#include "kmftech_types.h"

/*                         PWM  Global Declarations                           */
#define PWM_TOP_VALUE (4999)


/*                           Functions Prototypes                             */
void vd_bsc_pwm_init(void);
void vd_bsc_pwm_set(uint8 u8_pwm_id, 
                    uint16 u16_duty_cycle_us, 
                    uint16 u16_pwm_period_us);

/*                         Functions Implementation                           */

/*----------------------------------------------------------------------------*/
/*---------------------------- FUNCTION HEADER -------------------------------*/
/* Name       : vd_bsc_pwm_init                                               */
/* Description: Initialize the PWM channels                                   */
/* Input(s)   : Input  1: void                                                */
/* Output(s)  : Output 1: void                                                */
/* Author     : Karim Mansour                                                 */
/*----------------------------------------------------------------------------*/
void vd_bsc_pwm_init(void)
{
   /*Configure PWMs 16 bits A and B to be non-inverted PWM signal*/
   TCCR1A |= (1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);
   /*PRESCALER=64 MODE 14(FAST PWM)*/
   TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS11)|(1<<CS10); 
   /*TOP Value*/
   /* TOP Value = CPU_FREQ/(PCPU_DIVSOR*PWM_FREQ) - 1*/
   ICR1 = PWM_TOP_VALUE;
	/*Setting PWM pins to be output*/
   DDRB|=(1<<PB1)|(1<<PB2);
	/*Initialize the PWM to zero*/
	OCR1A = 0;
   OCR1B = 0;
}

/*----------------------------------------------------------------------------*/
/*---------------------------- FUNCTION HEADER -------------------------------*/
/* Name       : vd_bsc_pwm_set                                                */
/* Description: Initialize the PWM channels                                   */
/* Input(s)   : Input  1: uint8 - PWM Channel ID                              */
/* Input(s)   : Input  2: uint16 - PWM duty cycle in us                       */
/* Input(s)   : Input  3: uint16 - PWM period in us                           */
/* Output(s)  : Output 1: void                                                */
/* Author     : Karim Mansour                                                 */
/*----------------------------------------------------------------------------*/
void vd_bsc_pwm_set(uint8 u8_pwm_id, 
                    uint16 u16_duty_cycle_us, 
                    uint16 u16_pwm_period_us)
{
   uint16 u16Local_duty_register;
   uint32 u32Local_temp;
   u32Local_temp = (uint32)(PWM_TOP_VALUE+1) * (uint32)u16_duty_cycle_us;
   u32Local_temp = (uint32)(u32Local_temp / (uint32)u16_pwm_period_us);   
   u16Local_duty_register = (uint16) u32Local_temp;
  
   switch (u8_pwm_id)
   {
      case 1:
         OCR1A = u16Local_duty_register;         
         break;
      case 2:
         OCR1B = u16Local_duty_register;
         break;
      default:
         break;
   }
}

/*-----------------------------End Of File------------------------------------*/